import { Component, OnInit,Input,ElementRef,Injectable,ComponentFactoryResolver,ComponentFactory,ComponentRef,ChangeDetectorRef,ChangeDetectionStrategy} from '@angular/core';

@Component({
  selector: 'app-alert',
  templateUrl: './alert.component.html',
  styleUrls: ['./alert.component.css'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class AlertComponent implements OnInit {

  /**提示头部信息和提示信息*/
  @Input()title:string;
  @Input()message:string;

  constructor(private elementRef:ElementRef) {}

  ngOnInit() {

  }

  ngAfterViewInit(){

  }
  close(){
     window.document.body.removeChild(this.elementRef.nativeElement);
  }

}
/**用服务调用alert*/
@Injectable()
export class AlertService{
  
   constructor(private resolver:ComponentFactoryResolver){
        
   }
   
   /**提示框*/
   alert(title?:string,message?:string){

       let factory:ComponentFactory<AlertComponent> = this.resolver.resolveComponentFactory(AlertComponent);
       let alertRef:ComponentRef<AlertComponent> = factory.create(null);
           alertRef.instance.title=title||'提示框';
           alertRef.instance.message=message||'message';
           /**alertRef.changeDetectorRef.detectChanges();*/
           alertRef.hostView.detectChanges();
            console.dir(alertRef.instance);
          // alertRef.hostView.checkNoChanges();
           //alertRef.changeDetectorRef.markForCheck();
       window.document.body.appendChild(alertRef.location.nativeElement);
   }
}
